package com.example.futureai.service.impl;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.futureai.dto.DocVtsDto;
import com.example.futureai.dto.SendMsgDto;
import com.example.futureai.mapper.chat.UserSessionMapper;
import com.example.futureai.mapper.chat.UserSessionRecordMapper;
import com.example.futureai.po.ChatSessionRecord;
import com.example.futureai.po.UserSession;
import com.example.futureai.service.CharRecordService;
import com.example.futureai.utils.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @InterfaceName: CharRecordService
 * @Description:
 * @Author: 何荣
 * @Date: 2025/4/7
 */
@Slf4j
@Service
public class CharRecordServiceImpl implements CharRecordService {
    @Autowired
    private UserSessionMapper userSessionMapper;

    @Autowired
    private UserSessionRecordMapper userSessionRecordMapper;


    @Override
    public String saveCharRecord(String msgContent, SendMsgDto sendMsgDto) {
        //根据accessKey 检查是否存在对话
        //如果不存在，则插入对话
        UserSession userSession = userSessionMapper.selectById(sendMsgDto.getAccessKey());
        if(null == userSession){
            UserSession addUserSession = new UserSession();
            addUserSession.setId(sendMsgDto.getAccessKey());
            addUserSession.setModel("deepseek-r1");
            addUserSession.setName(sendMsgDto.getInputInfo());
            addUserSession.setUsername(sendMsgDto.getUserId());
            addUserSession.setCreateBy(sendMsgDto.getUserId());
            addUserSession.setUpdateBy(sendMsgDto.getUserId());
            userSessionMapper.insert(addUserSession);
        }
       //添加对话记录
        ChatSessionRecord addChatSessionRecord = new ChatSessionRecord();
        String msgId = UUID.randomUUID().toString();
        addChatSessionRecord.setId(msgId);
        addChatSessionRecord.setSessionId(sendMsgDto.getAccessKey());
        addChatSessionRecord.setQuestion(sendMsgDto.getInputInfo());
        addChatSessionRecord.setCreateBy(sendMsgDto.getUserId());
        addChatSessionRecord.setUpdateBy(sendMsgDto.getUserId());
        addChatSessionRecord.setContent(msgContent);
        //匹配到的文件
        List<DocVtsDto> files = sendMsgDto.getFiles();
        addChatSessionRecord.setFiles(JSONUtil.toJsonStr(files));
        addChatSessionRecord.setChatType(sendMsgDto.getChatType());
        userSessionRecordMapper.insert(addChatSessionRecord);
        return msgId;
    }

    @Override
    public List<UserSession> getCharSession() {
        QueryWrapper<UserSession> queryWrapper = new QueryWrapper<>();
        UserSession query = new UserSession();
        query.setUsername(TokenUtil.getCurrentUserId());
        queryWrapper.orderByDesc("create_time");
        queryWrapper.setEntity(query);
        //获取当前用户的所有对话
        return userSessionMapper.selectList(queryWrapper);
    }

    @Override
    public List<ChatSessionRecord> getCharRecordById(String accessKey) {
        QueryWrapper<ChatSessionRecord> queryWrapper = new QueryWrapper<>();
        ChatSessionRecord query = new ChatSessionRecord();
        query.setSessionId(accessKey);
        queryWrapper.orderByAsc("create_time");
        queryWrapper.setEntity(query);
        return userSessionRecordMapper.selectList(queryWrapper);
    }

    @Override
    public void updateCharRecord(String mesgId, String content) {
        ChatSessionRecord addChatSessionRecord = new ChatSessionRecord();
        addChatSessionRecord.setId(mesgId);
        addChatSessionRecord.setContent(content);
        userSessionRecordMapper.updateById(addChatSessionRecord);
    }


}
